


***********************************************
****** The Impact of Soft-Skills Training *****
******    for Entrepreneurs in Jamaica    *****
***********************************************


* This file merges the data from baseline, follow-up 1 and follow-up 2, and prepares them for the analysis


clear all
cap log close
set more off

cap cd "$directory"



************
*** Data ***
************

*** Merge baseline, follow-up 1 and follow-up 2 ***

* Add prefix fu1_ to all variables at fu1
use "$data_fin/fu1_data_final", clear
rename * fu1_*
rename fu1_respondent_id respondent_id
tempfile fu1
save `fu1'

* Add prefix fu2_ to all variables at fu2
use "$data_fin/fu2_data_final", clear
rename * fu2_*
rename fu2_respondent_id respondent_id
tempfile fu2
save `fu2'

* Load baseline data
use "$data_fin/bas_data_final", clear

* Merge with fu1 data
merge 1:1 respondent_id using `fu1', nogen assert(master match)

* Merge with fu2 data
merge 1:1 respondent_id using `fu2', nogen assert(master match)

* Drop duplicate variables from merge
drop fu?_strata fu?_treatment fu?_t1 fu?_t2


*** Generate attrition dummies ***

* Attrition in fu1
gen fu1_attriter=(fu1_merge_field!=1 & fu1_merge_field!=3)
lab var fu1_attriter "Attriter in FU1"

* Attrition in fu2
gen fu2_attriter=(fu2_merge_sensitizing!=1 & fu2_merge_sensitizing!=3)
lab var fu2_attriter "Attriter in FU2"

* Attrition in both waves
gen fu1_fu2_attriter=(fu1_attriter==1 & fu2_attriter==1)
lab var fu1_fu2_attriter "Attriter in FU1 and FU2"



**********************************
*** Variables for the analysis ***
**********************************

*** Manage missing values ***

* Define global macro with list of outcome variables
global all_outcomes has_business sales_profits_index business_practices_index personal_initiative_index inputs_index innovation loan_requested ///
	sales_lastm win_sales_lastm ihs_sales_lastm profits_lastm win_profits_lastm ihs_profits_lastm win_costs_lastm pos_profits_lastm busprac_* ///
	total_employees employees_fulltime employees_parttime

* Define global macro with list of covariates
global all_covariates female has_employees edu_sec_more course_kingston course_clarendon course_stthomas age black married num_children internet_access parents_entrep ///
	save_bank loan_bank loan_notaccess set_goal business_wchange satisfied_job reservation_wage personal_initiative perseverance locus_control risk_willing expenditures ///
	pre_course active_business busage_l1 formal_accounts informal_accounts register_bus sales_lastm profits_lastm innovation business_practices_index barriers_couple

* Replace missing values with 0 and generate dummies for missing values
foreach var of varlist $all_outcomes $all_covariates {
	cap confirm new variable m_`var'	
	if _rc continue								// if the variable is already in the dataset, the rest of the loop is skipped
	clonevar m_`var'=`var'
	replace m_`var'=0 if missing(`var')
	gen d_`var'=missing(`var')
	lab var d_`var' "`: var lab `var'' (miss. indicator)"
}


*** Compute weights for attrition ***

* Include new variables for missing values in new global macros
global all_covariates_m
global all_covariates_d
foreach var of varlist $all_covariates {
	global all_covariates_m "$all_covariates_m m_`var'"
	global all_covariates_d "$all_covariates_d d_`var'"
}

* Estimate p-scores separately by wave and by treatment
forvalues i=1/2 {
	gen fu`i'_natt=1-fu`i'_attriter
	logit fu`i'_natt $all_covariates_m $all_covariates_d if treatment==0, asis
	predict fu`i'_control_pscore if treatment==0
	logit fu`i'_natt $all_covariates_m $all_covariates_d if treatment!=0, asis
	predict fu`i'_treat_pscore if treatment!=0
}

* Combine p-scores and generate inverse probability of attrition for each wave
forvalues i=1/2 {
	egen fu`i'_pscore=rowtotal(fu`i'_control_pscore fu`i'_treat_pscore), missing
	gen fu`i'_att_weight=1/fu`i'_pscore
	lab var fu`i'_att_weight "Inverse probability of attrition weight"
}

* Drop extra variables
drop fu?_natt fu*pscore



**************
*** Saving ***
**************

* Save data
compress
save "$data_fin/Jamaica_data_final", replace


